using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;

namespace UseCaseManager.UI
{
    /// <summary>
    /// A read-only grid control used for adding and editing Releases.
    /// </summary>
    public class ReleasesGridControl : UserControlWin, IFormControl
    {
        public ReleasesGridControl():this(GlobalUIRegistry.ControlFactory)
        {
        }

        public ReleasesGridControl(IControlFactory controlFactory)
        {
            ControlFactory = controlFactory;

            // Create the read-only grid
            GridSelector = ControlFactory.CreateReadOnlyGridControl();
            
            // Used to specify a custom UI def
            GridSelector.Initialise(ClassDef.Get<Release>(), "default");

            // Load the collection of Releases.  You can set the criteria and order by values here.
            BusinessObjectCollection<Release> sampleCollection = Broker.GetBusinessObjectCollection<Release>("", "");
            GridSelector.BusinessObjectCollection =  sampleCollection;

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            GridSelector.FilterControl.Visible = false;
            Buttons = CreateCreateDropsButton();
            //releaseGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            IReadOnlyGridButtonsControl buttons = ((ReadOnlyGridControlWin)  GridSelector).Buttons;
            buttons.ShowDefaultDeleteButton = true;
            // Add the grid to the control so that it fills the control
            BorderLayoutManager layoutManager = ControlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(GridSelector, BorderLayoutManager.Position.Centre);
        }
        public IButtonGroupControl Buttons { get; private set; }

        private IButtonGroupControl CreateCreateDropsButton()
        {
            IReadOnlyGridButtonsControl buttons = ((ReadOnlyGridControlWin)GridSelector).Buttons;
            buttons.AddButton("CreateDrops", "Create Drops", delegate
                                     {
                                         Release release = (Release) this.GridSelector.SelectedBusinessObject;
                                         if (release != null)
                                         {
                                             release.CreateDrops();
                                         }
                                     });
            return buttons;
        }
        /// <summary>
        /// Returns the ReadOnlyGridControl that is being used for showing and editing Drops in this control
        /// </summary>
        public IReadOnlyGridControl GridSelector { get; private set; }

        private IControlFactory ControlFactory { get; set; }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form) { }
    }
}